//
//  TenTableViewController.m
//  天下事
//
//  Created by dlios on 15-7-11.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TenTableViewController.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "NewsCell.h"
#import "DataHandel.h"
#import "NetWorkTools.h"
#import "SecMyCell.h"
#import "ThirdMyCell.h"
#import "BigImageCell.h"
#import "BigImageCollectionViewcell.h"
#import "BigImageCellB.h"
#import "Headerdetails.h"
#import "Celldetails.h"


@interface TenTableViewController()<pushDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectview;
@property(nonatomic, retain)NSMutableArray *temparr;
@property(nonatomic,assign)BOOL update;
@property(nonatomic, retain)BigImageCell *cell1;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation TenTableViewController
static NSInteger count = 0;

- (void)viewDidLoad {
    [super viewDidLoad];
    
   [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    self.update = YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"SecMyCell" bundle:nil] forCellReuseIdentifier:@"SecMyCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ThirdMyCell" bundle:nil] forCellReuseIdentifier:@"ThirdMyCell"];
    [self.tableView registerClass:[BigImageCell class] forCellReuseIdentifier:@"BigImageCell"];
    [self.tableView registerClass:[BigImageCellB class] forCellReuseIdentifier:@"BigImageCellB"];
    
    BigImageCell *BI = [[BigImageCell alloc]init];
    BI.Mydelegate = self;
    
    self.pagecontroler = [[UIPageControl alloc]init];
    self.pagecontroler.currentPageIndicatorTintColor = [UIColor cyanColor];
    self.pagecontroler.pageIndicatorTintColor = [UIColor blackColor];
 //   self.pagecontroler.numberOfPages = 2;
    [self.view addSubview:self.pagecontroler];
    self.pagecontroler.currentPage = 0;
    CGRect rect = CGRectMake(300, 200, 100, 20);
       self.pagecontroler.frame = rect;
    self.pagecontroler.hidden = YES;
    


    
    

}
- (void)viewWillAppear:(BOOL)animated{
    if (self.update == YES) {
        [self.tableView headerBeginRefreshing];
        self.update = NO;
    }

}
- (BOOL)prefersStatusBarHidden{
    return YES;
}
- (void)headerRefresh{
    NSString *Astring = [NSString stringWithFormat:@"/nc/article/%@/0-20.html", self.urlString];
    [self loadDataForType:1 withURL:Astring];
    
    
}
- (void)footerRefresh{
    NSString *allUrlstring = [NSString stringWithFormat:@"/nc/article/%@/%ld-20.html",self.urlString,(self.arrayList.count - self.arrayList.count%10)];
    [self loadDataForType:2 withURL:allUrlstring];
    
}

- (void)loadDataForType:(int)type withURL:(NSString *)allUrlstring
{


    [[[NetWorkTools sharNetworkTools]GET:allUrlstring parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary* responseObject) {
        NSString *key = [responseObject.keyEnumerator nextObject];
        NSArray *temArray = responseObject[key];
        NSMutableArray *arrayM = [[NSMutableArray alloc]init];
        //快速枚举
        [temArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
            NewsModel *news = [NewsModel newsModelwithDic:obj];
            [arrayM addObject:news];
            
        }];
        if (type == 1) {
        self.arrayList = arrayM;
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
        self.arr = [NSMutableArray array];
        for (int i = 0; i < self.arrayList.count; i++) {
            if ([self.arrayList[i] hasHead ]) {
                NSMutableArray *temarr = [[self.arrayList[i] imgextra]mutableCopy];
                for (NSDictionary *dic in temarr) {
                    [self.arr addObject:dic];
                }
            }
        }
        }
        if (type == 2) {
        [self.arrayList addObjectsFromArray:arrayM];
        [self.tableView reloadData];
        [self.tableView footerEndRefreshing];
        }
 
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }] resume];
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModel *nm = self.arrayList[indexPath.row];
    if (nm.hasHead && nm.photosetID && nm.imgextra  != nil) {
        self.cell1 = [tableView dequeueReusableCellWithIdentifier:@"BigImageCell" forIndexPath:indexPath];
        //给headercollectionview赋值
        self.cell1.collectview.delegate = self;
        self.cell1.collectview.dataSource = self;
        self.pagecontroler.hidden = NO;
        self.pagecontroler.numberOfPages = self.arr.count;
        [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoscroll:) userInfo:nil repeats:YES];
        
        return self.cell1;
        
    }else if(nm.hasHead){
       BigImageCellB *cell4 = [tableView dequeueReusableCellWithIdentifier:@"BigImageCellB" forIndexPath:indexPath];
       cell4.nm = nm;
        self.pagecontroler.hidden = YES;
        return cell4;
    }
    else if (nm.imgextra) {
        ThirdMyCell *cell3= [tableView dequeueReusableCellWithIdentifier:@"ThirdMyCell" forIndexPath:indexPath];
        cell3.nm = nm;

        return cell3;
    }else{
        SecMyCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"SecMyCell" forIndexPath:indexPath];
        cell2.newsmodel = nm;
        return cell2;
    }
    return nil;
}

- (void)autoscroll:(NSTimer *)time {
    count++;
    NSInteger pagenbr = count % _arr.count;
    self.pagecontroler.currentPage = pagenbr;
    NSIndexPath *indexpath = [NSIndexPath indexPathForItem:pagenbr inSection:0];
   [self.cell1.collectview scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}

//collectionview的代理方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        BigImageCollectionViewcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BigImageCollectionViewcell" forIndexPath:indexPath];
        [cell.bigImg sd_setImageWithURL:[NSURL URLWithString:[self.arr[indexPath.item] objectForKey:@"imgsrc"]]];
        cell.BigImagetitle.text = [[self.arrayList firstObject] title];
        return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    Headerdetails *dh = [[Headerdetails alloc]init];
    [self presentViewController:dh animated:NO completion:^{
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModel *newmodel = self.arrayList[indexPath.row];
    if (newmodel.hasHead && newmodel.photosetID) {
        return 240;
    }
     if(newmodel.hasHead){
        return 200;
    }
     if (newmodel.imgextra) {
        return 130;
    }else
        return 100;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger xx = self.cell1.collectview.contentOffset.x / self.view.frame.size.width;
    self.pagecontroler.currentPage = xx;
 
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NewsModel *nm = self.arrayList[indexPath.row];
    
    if (nm.hasHead && nm.photosetID) {
   
    }
    else if(nm.hasHead){
  
    }
    else if (nm.imgextra) {
        
        
        
    }else{
        Celldetails *cell = [[Celldetails alloc]init];
        cell.cellString = nm.url_3w;
        
        cell.docidd = nm.docid;
       [self presentViewController:cell animated:NO completion:^{
            
        }];
    }
}

- (void)pushViewController{
    Headerdetails *hd = [[Headerdetails alloc]init];
    [self presentViewController:hd animated:NO completion:^{
        
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayList.count;
}




@end
